package xworker.javafx.control;

import javafx.scene.control.ContextMenu;
import javafx.scene.control.Control;
import javafx.scene.control.MenuItem;
import org.xmeta.ActionContext;
import org.xmeta.Thing;
import xworker.javafx.beans.property.PropertyFactory;

public class ContextMenuActions {
    static{
        PropertyFactory.regist(ContextMenu.class, "onActionProperty", o -> {
            ContextMenu obj = (ContextMenu) o;
            return obj.onActionProperty();
        });
    }

    public static void init(ContextMenu box, Thing thing, ActionContext actionContext) {
        PopupControlActions.init(box, thing, actionContext);

    }

    public static ContextMenu create(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        Object parent = actionContext.getObject("parent");

        ContextMenu box = new ContextMenu();
        init(box, self, actionContext);
        actionContext.g().put(self.getMetadata().getName(), box);

        if(parent instanceof Control){
            ((Control) parent).setContextMenu(box);
        }

        actionContext.peek().put("parent", box);
        for(Thing child : self.getChilds()) {
            Object obj = child.doAction("create", actionContext);
            if(obj instanceof MenuItem){
                box.getItems().add((MenuItem) obj);
            }
        }

        return box;
    }
}
